package com.fyx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fyx.annotation.Log;
import com.fyx.entity.Entity;
import com.fyx.entity.MaiAdInfo;
import com.fyx.entity.MatchWinEntity;
import com.fyx.entity.TotalTicketLottery;
import com.fyx.service.MatchService;
import com.fyx.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "比赛功能相关")
@RestController
@RequestMapping("/api/match")
@RequiredArgsConstructor
public class MatchController {

    @Autowired
    private MatchService matchService;

    @Log("比赛兑换清单")
    @ApiOperation("比赛兑换清单")
    @GetMapping(value="getMatchReceiveList")
    @PreAuthorize("@el.check('getMatchReceive:list')")
    public ResponseEntity<Object> getMatchList(Entity entity, Pageable pageable){
        Map mapResult = matchService.getMatchList(entity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("比赛场胜负场")
    @ApiOperation("比赛场胜负场")
    @GetMapping(value="getMatchTurnWin")
    @PreAuthorize("@el.check('getMatchTurnWin:list')")
    public ResponseEntity<Object> getMatchTurnWin(MatchWinEntity matchWinEntity, Pageable pageable){
        Map mapResult = matchService.getMatchTurnWin(matchWinEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @PostMapping(value = "getMatchTurnWinMore")
    @PreAuthorize("@el.check('getMatchTurnWin:list')")
    public List<MatchWinEntity> getMatchTurnWinMore(@Validated @RequestBody JSONObject jsonObject){
        String channel= jsonObject.getString("channel");
        Integer time=Integer.valueOf(jsonObject.getString("time")) ;
        List<MatchWinEntity> list = matchService.getMatchTurnWinMore(time,channel);
        return list;
    }

    @Log("修改兑换")
    @ApiOperation("修改兑换")
    @PutMapping(value="editMatchReceive")
    public ResponseEntity<Object> editMatchReceive(@Validated @RequestBody Entity entity){
       Boolean b= matchService.editMatchReceive(entity);
       if(b){
        return new ResponseEntity<>(HttpStatus.OK);
       }else {
           return new ResponseEntity<>(HttpStatus.NO_CONTENT);
       }
    }


    @GetMapping(value="getMatchTicketLotteryList")
    @PreAuthorize("@el.check('getMatchTicketLottery:list')")
    public Map getMatchTicketLotteryList(TotalTicketLottery totalTicketLottery, Pageable pageable){
        Map mapResult = matchService.getMatchTicketLotteryList(totalTicketLottery,pageable);
        return mapResult;
        //new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @PostMapping(value = "getTicketLotteryTree")
    @PreAuthorize("@el.check('getMatchTicketLottery:list')")
    public List<MatchWinEntity> getMatchTicketLottery(@Validated @RequestBody JSONObject jsonObject){
        String channel = jsonObject.getString("channel");
        Integer time = Integer.valueOf(jsonObject.getString("time")) ;
        String flag = jsonObject.getString("flag");
        List<MatchWinEntity> list = matchService.getTicketLotteryTree(time,channel,flag);
        return list;
    }

}
